package com.ws.universal.tools;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ws.universal.tools.utils.ToastUtils;
import com.ws.universal.tools.wsui.help.TouchableSpan;
import com.ws.universal.tools.wsui.linktextview.LinkTextView;
import com.ws.universal.tools.wsui.widget.QMUIRoundButton;

/**
 * 控件使用示例
 */
public class DemoActivity extends AppCompatActivity {
    private QMUIRoundButton alpha_button2;
    private LinkTextView tvTest2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);
        bindView();
    }

    private void bindView() {
        alpha_button2 = findViewById(R.id.alpha_button2);
        alpha_button2 = findViewById(R.id.alpha_button2);
        //带点击效果
        alpha_button2.setChangeAlphaWhenPress(true);



        /*
        使用带超链接文本时,需要在主工程 的themes中添加
        <item name="app_skin_span_normal_text_color">@color/app_color_blue</item>
        <item name="app_skin_span_pressed_text_color">@color/app_color_blue_pressed</item>
        <item name="app_skin_span_normal_bg_color">@color/transparent</item>
        <item name="app_skin_span_pressed_bg_color">@color/_config_color_gray_6</item>
       */
        tvTest2 = findViewById(R.id.tvTest2);
        String test = "百度一下:默认情况下，如果textView添加了clickSpan,#谷歌浏览器#那么textView的事件将无法冒泡到父级。但在某些场景（例如微博评论需要整条可点），我们需要将事件冒泡给父元素进行处理。#ed浏览器#";
        tvTest2.setMovementMethodDefault();
        tvTest2.setNeedForceEventToParent(true);
        tvTest2.setText(generateSp(tvTest2, test));

    }

    private SpannableString generateSp(TextView tv, String text) {
        String highlight1 = "百度一下";
        String highlight2 = "#谷歌浏览器#";
        String highlight3 = "#ed浏览器#";
        SpannableString sp = new SpannableString(text);
        int start = 0, end;
        int index;
        while ((index = text.indexOf(highlight1, start)) > -1) {
            end = index + highlight1.length();
            sp.setSpan(new TouchableSpan(tv,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_bg_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_bg_color) {
                @Override
                public void onSpanClick(View widget) {

                    ToastUtils.showLong("click @highlight1");
                }
            }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            start = end;
        }

        start = 0;
        while ((index = text.indexOf(highlight2, start)) > -1) {
            end = index + highlight2.length();
            sp.setSpan(new TouchableSpan(tv,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_bg_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_bg_color) {
                @Override
                public void onSpanClick(View widget) {
                    ToastUtils.showLong("click @highlight2");
                }
            }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            start = end;
        }
        start = 0;
        while ((index = text.indexOf(highlight3, start)) > -1) {
            end = index + highlight3.length();
            sp.setSpan(new TouchableSpan(tv,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_text_color,
                    com.ws.universal.tools.R.attr.app_skin_span_normal_bg_color,
                    com.ws.universal.tools.R.attr.app_skin_span_pressed_bg_color) {
                @Override
                public void onSpanClick(View widget) {
                    ToastUtils.showLong("click @highlight2");
                }
            }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            start = end;
        }
        return sp;
    }
}
